(function (angular) {
    'use strict';

    angular.module('ytong.controllers').controller('messagesCtrl', messagesCtrl);

    function messagesCtrl(feedbacksManager, ModalService){

        var NOT_READ = "0";
        var READ = "1";

        var _self = this;
        _self.showAll = false;

        this.messages = feedbacksManager.getMessages();

        this.filterMessages = function(message){
            if(_self.showAll){
                return true;
            }

            if(message.response_read === NOT_READ){
                return true;
            }

            return false;
        };

        this.getMessageStatus = function(message){
            return feedbacksManager.getMessageStatus(message);
        };

        this.readMessage  = function(message){
            ModalService.showModal({
                templateUrl: 'readMessage',
                controller: 'readMessageController as rmc',
                inputs:{
                    message: message
                }
            })
                .then(function(modal){
                    modal.element.modal();
                    modal.close.then(function(result){


                        message.response_read = READ;
                        feedbacksManager.updateFeedback(message, true);

                    });
                });
        };

    };

    angular.module('ytong.controllers').controller('readMessageController', readMessageController);

    function readMessageController(close, message){
        var OPEN = "0";
        var CLOSED = "1";
        var REVIEWING = "2";

        this.message = angular.copy(message);
        this.message.status_readable = "";
        switch(message.status){
            case OPEN: {
                this.message.status_readable = "Open";
                break;
            }
            case CLOSED: {
                this.message.status_readable = "Closed";
                break;
            }
            case REVIEWING: {
                this.message.status_readable = "Reviewing";
                break;
            }
        }

        this.close = function(action) {
            var result = {};
            result.action = action;
            result.message = this.message;
            close(result, 500); // close, but give 500ms for bootstrap to animate
        };

    };

}(angular));